package com.start.common.dic;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.start.common.configs.StartTokenConfig;
import com.start.common.dataImport.ExcelActionType;
import com.start.common.exceptions.StartBusException;
import com.start.common.tools.ClassTools;

import java.lang.reflect.Field;
import java.util.*;

public class DictMapperParamsUtils {


    /**
     * 获取导出参数
     *
     * @param clazz
     * @param actionType false 导出，true 导入
     * @param <T>
     * @return
     */
    public static <T> DictMapperParams getExcelMapperParams(Class<? extends T> clazz, ExcelActionType actionType) {
        List<Field> fields = ClassTools.getEffectiveFields(clazz);
        return getExcelMapperParams(fields, actionType);
    }


    /**
     * 获取导入参数
     *
     * @param fields     成员变量
     * @param actionType false 导出，true 导入
     *                   false      {field.getName():annotation.title()}
     *                   true        {annotation.title():field.getName()}
     * @return
     */
    public static DictMapperParams getExcelMapperParams(List<Field> fields, ExcelActionType actionType) {
        return getExcelMapperParams(fields, actionType, true);
    }

    public static DictMapperParams getExcelMapperParams(List<Field> fields, ExcelActionType actionType, boolean autoSort) {
        if (fields == null || fields.size() < 1) return null;
        Map<String, StartBaseDicMapperHandler> map = new LinkedHashMap<>();
        Map<String, String> head = new LinkedHashMap<>();
        DictMapperParams dictMapperParams = new DictMapperParams();
        dictMapperParams.setHandlerFiled(map);
        dictMapperParams.setHeaderAlias(head);
        List<EntityDictMapperHandler> mapperHandlers = new ArrayList<>();
        for (Field field : fields) {
            DictMapper annotation = field.getAnnotation(DictMapper.class);
            if (annotation == null) continue;
            if (!map.containsKey(field.getName())) {
                EntityDictMapperHandler excelMapperHandler = new EntityDictMapperHandler(annotation, field, actionType);
                map.put(field.getName(), excelMapperHandler);
                mapperHandlers.add(excelMapperHandler);
            }
        }
        if (autoSort) {
            mapperHandlers.sort(Comparator.comparingInt(EntityDictMapperHandler::getSort));
        }
        for (EntityDictMapperHandler handler : mapperHandlers) {
            String key = actionType == ExcelActionType.EXCEL_IMPORT ? handler.getTitle() : handler.getFieldName();
            String value = actionType == ExcelActionType.EXCEL_IMPORT ? handler.getFieldName() : handler.getTitle();
            if (head.containsKey(key)) {
                throw new StartBusException("表头重复 " + key);
            }
            head.put(key, value);
        }
        return dictMapperParams;
    }


    public static DictMapperParams getExcelMapperParams(JSONObject formOption, ExcelActionType actionType, StartTokenConfig tokenConfig, String userToken) {
        if (!formOption.containsKey("column")) {
            return null;
        }
        Map<String, StartBaseDicMapperHandler> map = new LinkedHashMap<>();
        Map<String, String> head = new LinkedHashMap<>();
        JSONArray column = formOption.getJSONArray("column");
        for (int i = 0; i < column.size(); i++) {
            JSONObject item = column.getJSONObject(i);
            boolean isExcel = item.containsKey("excel") ? item.getBoolean("excel") : true;
            if (!isExcel) continue;
            StartDynamicFormMapperHandler handler = new StartDynamicFormMapperHandler(item, actionType, tokenConfig, userToken);
            map.put(handler.getFieldName(), handler);
            String key = actionType == ExcelActionType.EXCEL_IMPORT ? handler.getTitle() : handler.getFieldName();
            String value = actionType == ExcelActionType.EXCEL_IMPORT ? handler.getFieldName() : handler.getTitle();
            if (head.containsKey(key)) {
                throw new StartBusException("表头重复 " + key);
            }
            head.put(key, value);
        }
        DictMapperParams dictMapperParams = new DictMapperParams();
        dictMapperParams.setHandlerFiled(map);
        dictMapperParams.setHeaderAlias(head);
        return dictMapperParams;
    }

}
